using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Commonplace.Domain;
using System.Drawing;
using Commonplace.Properties;

namespace Commonplace
{
    public class DetailControl : UserControl
    {
   		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox _name;
		private System.Windows.Forms.TextBox _description;
		private System.Windows.Forms.TextBox _source;
		private System.Windows.Forms.ComboBox _license;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox _categories;
		private System.ComponentModel.IContainer components;

		DetailRecord _detail = null;
		private System.Windows.Forms.Button _remove;
		private System.Windows.Forms.Label _status;
		bool _disablePopulate = false;
		private System.Windows.Forms.ToolTip _toolTip;
		private System.Windows.Forms.CheckBox _watch;
        private TextBox _author;
        private Label label6;
        private Label label7;
        private TextBox _date;
		bool _disableGather = false;


        public DetailControl()
        {
            InitializeComponent();
        }

		public DetailControl(DetailRecord detail)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

            List<License> licenses = CachedConfig.Instance.Licenses;
            string defaultLicense = CachedConfig.Instance.DefaultLicense;
            _license.Items.AddRange(licenses.ToArray());

            if ("" != defaultLicense)
                detail.License = defaultLicense;

			_detail = detail;

			PopulateFields();

			_detail.Updated += new EventHandler(_detail_Updated);
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DetailControl));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this._name = new System.Windows.Forms.TextBox();
            this._description = new System.Windows.Forms.TextBox();
            this._source = new System.Windows.Forms.TextBox();
            this._license = new System.Windows.Forms.ComboBox();
            this.label5 = new System.Windows.Forms.Label();
            this._categories = new System.Windows.Forms.TextBox();
            this._status = new System.Windows.Forms.Label();
            this._toolTip = new System.Windows.Forms.ToolTip(this.components);
            this._watch = new System.Windows.Forms.CheckBox();
            this._remove = new System.Windows.Forms.Button();
            this._author = new System.Windows.Forms.TextBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this._date = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(168, 7);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 17);
            this.label1.TabIndex = 1;
            this.label1.Text = "Name:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(168, 25);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 20);
            this.label2.TabIndex = 2;
            this.label2.Text = "Description:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(168, 71);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(64, 20);
            this.label3.TabIndex = 3;
            this.label3.Text = "Source:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label4
            // 
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.Location = new System.Drawing.Point(168, 136);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(64, 21);
            this.label4.TabIndex = 4;
            this.label4.Text = "License:";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _name
            // 
            this._name.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._name.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._name.Location = new System.Drawing.Point(232, 8);
            this._name.Name = "_name";
            this._name.Size = new System.Drawing.Size(272, 18);
            this._name.TabIndex = 0;
            this._name.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // _description
            // 
            this._description.AcceptsReturn = true;
            this._description.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._description.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._description.Location = new System.Drawing.Point(232, 28);
            this._description.Multiline = true;
            this._description.Name = "_description";
            this._description.Size = new System.Drawing.Size(272, 43);
            this._description.TabIndex = 1;
            this._description.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // _source
            // 
            this._source.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._source.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._source.Location = new System.Drawing.Point(232, 74);
            this._source.Name = "_source";
            this._source.Size = new System.Drawing.Size(272, 18);
            this._source.TabIndex = 2;
            this._source.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // _license
            // 
            this._license.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._license.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this._license.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._license.Location = new System.Drawing.Point(232, 136);
            this._license.Name = "_license";
            this._license.Size = new System.Drawing.Size(272, 20);
            this._license.TabIndex = 6;
            this._license.SelectedIndexChanged += new System.EventHandler(this.Field_Changed);
            this._license.TextUpdate += new System.EventHandler(this.Field_Changed);
            // 
            // label5
            // 
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(168, 91);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(64, 20);
            this.label5.TabIndex = 9;
            this.label5.Text = "Categories:";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _categories
            // 
            this._categories.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._categories.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._categories.Location = new System.Drawing.Point(232, 94);
            this._categories.Name = "_categories";
            this._categories.Size = new System.Drawing.Size(272, 18);
            this._categories.TabIndex = 3;
            this._toolTip.SetToolTip(this._categories, "Enter category names, separated by pipe symbols, e.g. Automobile|New York|UnitedS" +
                    "tates");
            this._categories.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // _status
            // 
            this._status.Location = new System.Drawing.Point(8, 136);
            this._status.Name = "_status";
            this._status.Size = new System.Drawing.Size(72, 23);
            this._status.TabIndex = 12;
            this._status.Text = "Not uploaded";
            this._status.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _watch
            // 
            this._watch.Appearance = System.Windows.Forms.Appearance.Button;
            this._watch.FlatAppearance.BorderSize = 0;
            this._watch.FlatAppearance.CheckedBackColor = System.Drawing.Color.Transparent;
            this._watch.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this._watch.Image = global::Commonplace.Properties.Resources.Watch_Disabled;
            this._watch.Location = new System.Drawing.Point(96, 136);
            this._watch.Name = "_watch";
            this._watch.Size = new System.Drawing.Size(24, 24);
            this._watch.TabIndex = 7;
            this._toolTip.SetToolTip(this._watch, "Watch this file after upload.");
            this._watch.CheckedChanged += new System.EventHandler(this._watch_CheckedChanged);
            // 
            // _remove
            // 
            this._remove.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            this._remove.FlatAppearance.BorderSize = 0;
            this._remove.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this._remove.Image = ((System.Drawing.Image)(resources.GetObject("_remove.Image")));
            this._remove.Location = new System.Drawing.Point(128, 136);
            this._remove.Name = "_remove";
            this._remove.Size = new System.Drawing.Size(24, 24);
            this._remove.TabIndex = 8;
            this._toolTip.SetToolTip(this._remove, "Remove this file from the list.");
            this._remove.Click += new System.EventHandler(this._remove_Click);
            // 
            // _author
            // 
            this._author.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._author.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._author.Location = new System.Drawing.Point(232, 115);
            this._author.Name = "_author";
            this._author.Size = new System.Drawing.Size(111, 18);
            this._author.TabIndex = 4;
            this._author.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // label6
            // 
            this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label6.Location = new System.Drawing.Point(168, 114);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(64, 20);
            this.label6.TabIndex = 15;
            this.label6.Text = "Author:";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label7
            // 
            this.label7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label7.Location = new System.Drawing.Point(349, 114);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(34, 20);
            this.label7.TabIndex = 16;
            this.label7.Text = "Date:";
            this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // _date
            // 
            this._date.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this._date.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this._date.Location = new System.Drawing.Point(389, 115);
            this._date.Name = "_date";
            this._date.Size = new System.Drawing.Size(115, 18);
            this._date.TabIndex = 5;
            this._date.TextChanged += new System.EventHandler(this.Field_Changed);
            // 
            // DetailControl
            // 
            this.Controls.Add(this._date);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label6);
            this.Controls.Add(this._author);
            this.Controls.Add(this._watch);
            this.Controls.Add(this._status);
            this.Controls.Add(this._remove);
            this.Controls.Add(this._categories);
            this.Controls.Add(this.label5);
            this.Controls.Add(this._license);
            this.Controls.Add(this._source);
            this.Controls.Add(this._description);
            this.Controls.Add(this._name);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "DetailControl";
            this.Size = new System.Drawing.Size(512, 160);
            this.Load += new System.EventHandler(this.Detail_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void PopulateFields()
		{
            if (true == _disablePopulate)
                return;

			_disableGather = true;
			_name.Text = _detail.Name;
			_description.Text = _detail.Description;
			_source.Text = _detail.Source;
			_categories.Text = _detail.Categories;
			_license.Text = _detail.License;
            _author.Text = _detail.Author;
            _date.Text = _detail.Date;

			if (true == _detail.Uploaded)
				_status.Text = "Uploaded";
			else
				_status.Text = "Not uploaded";

			_watch.Checked = _detail.Watch;
			_disableGather = false;
		}

		private void GatherFields()
		{
			if (true == _disableGather)
				return;

			_disablePopulate = true;
			_detail.Name = _name.Text;
			_detail.Description = _description.Text;
			_detail.Source = _source.Text;
			_detail.Categories = _categories.Text;
            _detail.License = _license.Text;
            _detail.Author = _author.Text;
            _detail.Date = _date.Text;

			_detail.Watch = _watch.Checked;
			_disablePopulate = false;
		}

        private void Field_Changed(object sender, System.EventArgs e)
        {
            GatherFields();
        }

		private void _detail_Updated(object sender, EventArgs e)
		{
			if (true == _disablePopulate)
				return;

			PopulateFields();
		}

		private void _remove_Click(object sender, System.EventArgs e)
		{
            OnRemove();
		}

		private void Detail_Load(object sender, System.EventArgs e)
		{
			_name.Focus();
            _name.SelectionLength = 0;
		}

		private void _watch_CheckedChanged(object sender, System.EventArgs e)
		{
			GatherFields();		

			if (true == _watch.Checked)
			{
                _watch.Image = Resources.Watch_Enabled;
				_toolTip.SetToolTip(_watch, "Click to disable watching this file after upload.");
			}
			else
			{
                _watch.Image = Resources.Watch_Disabled;
				_toolTip.SetToolTip(_watch, "Watch this file after upload.");
			}
		}


        public virtual DetailRecord Detail { get { return _detail; } }

        public event EventHandler Remove;

        public void OnRemove()
        {
            if (null != Remove)
                Remove(this, EventArgs.Empty);
        }
    }
}
